package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.remote.LocatorRecordMapper;
import com.zhijian.medical.entity.po.LocatorRecord;
import com.zhijian.medical.entity.vo.request.LocatorRecordSearchVo;
import com.zhijian.medical.entity.vo.response.LocatorRecordVo;
import com.zhijian.medical.service.LocatorRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Lac 获取位置业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-13 15:06:10
 */
@Slf4j
@Service
public class LocatorRecordServiceImpl implements LocatorRecordService {

    @Autowired
    private LocatorRecordMapper mapper;

    @Override
    public LocatorRecord selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public LocatorRecordVo selectVoById(Integer id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(LocatorRecord locatorRecord) {
        Date now = new Date();
        locatorRecord.setCreateTime(now);
        return mapper.insert(locatorRecord) > 0 ? locatorRecord.getId() : 0L;
    }

    @Override
    public boolean update(LocatorRecord locatorRecord) {
        return mapper.updateByPrimaryKey(locatorRecord) != 0;
    }

    @Override
    public Integer count(LocatorRecordSearchVo locatorRecordSearchVo) {
        return mapper.count(locatorRecordSearchVo);
    }

    @Override
    public List<LocatorRecordVo> selectList(LocatorRecordSearchVo locatorRecordSearchVo) {
        return mapper.selectList(locatorRecordSearchVo);
    }

    @Override
    public Page<LocatorRecordVo> selectPage(LocatorRecordSearchVo locatorRecordSearchVo) {
        //先直接查总数
        int count = count(locatorRecordSearchVo);
        //分页
        Page<LocatorRecordVo> page = new Page<>(true, count, locatorRecordSearchVo.getPageNum(), locatorRecordSearchVo.getPageSize());
        locatorRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<LocatorRecordVo> responseVos = this.selectList(locatorRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}